package com.aigou.admin.entity.vo;


import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.IOException;
import java.io.Serializable;

/**
 *  * 返回给前端的统一格式
 * @Author: Hor
 * @Date: 2024/12/4 08:17
 * @Version: 1.0
 */
@Data
@AllArgsConstructor
@NoArgsConstructor
public class ResponseVo<T> implements Serializable {
    // 序列化的，大家不用纠结注意
    private static final long serialVersionUID = 1L;

    //响应状态码
    private Integer code;
    //描述信息
    private String massage;
    //绑定数据
    private T data;

    public ResponseVo(ResponseStatusEnum responseStatusEnum, T data) {
        this.code = responseStatusEnum.getCode();
        this.massage = responseStatusEnum.getMessage();
    }


    /**
     * * 业务成功返回业务代码和描述信息
     */
    public static ResponseVo<Void> buildSuccess() {
        return new ResponseVo<>(ResponseStatusEnum.SUCCESS, null);
    }


    /**
     * 业务成功返回业务代码,描述和返回的参数
     */
    public static <T> ResponseVo<T> buildSuccess(T data) {
        return new ResponseVo<>(ResponseStatusEnum.SUCCESS, data);
    }

    /**
     * 业务成功返回业务代码,描述和返回的参数
     */
    public static <T> ResponseVo<T> buildSuccess(ResponseStatusEnum resultStatus, T data) {
     if (resultStatus == null) {
        return buildSuccess(data);
     }
     return new ResponseVo<>(resultStatus, data);
   }

    /**
     * 业务异常返回业务代码和描述信息
     */
    public static <T> ResponseVo<T> buildFail() {
        return new ResponseVo<T>(ResponseStatusEnum.FAIL, null);
    }


    /**
     * 业务异常返回业务代码,描述和返回的参数
     */
    public static <T> ResponseVo<T> buildFail(T data) {
        return new ResponseVo<>(ResponseStatusEnum.FAIL, data);
    }

    /**
     * 业务异常返回业务代码,描述和返回的参数
     */
    public static <T> ResponseVo<T> buildFail(ResponseStatusEnum resultStatus, T data) {
        if (resultStatus == null) {
            return new ResponseVo<T>(ResponseStatusEnum.FAIL, null);
        }
        return new ResponseVo<>(resultStatus, data);
    }


}